//
//  NEDConsoleMenuViewController.m
//  NEDBugConsole_Example
//
//  Created by 98du on 2021/12/14.
//  Copyright © 2021 林宁宁. All rights reserved.
//

#import "NEDConsoleMenuViewController.h"


#import "NEDConsoleMenuItem.h"
#import "NEDConsoleConstant.h"
#import "NEDConsole.h"

@interface NEDConsoleMenuViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView  *tableView;
@property(nonatomic,strong) NSArray <NEDConsoleMenuItem *>*dataSource;

@end

@implementation NEDConsoleMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.navigationItem.title = @"调试工具";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"❌" style:(UIBarButtonItemStyleDone) target:self action:@selector(closeAction)];
    
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setTranslucent:NO]; // 设置navigationBar的透明效果
    [self.navigationController.navigationBar setTintColor:[UIColor blueColor]];
    
    NSDictionary *attrs = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18],
                            NSForegroundColorAttributeName:[UIColor blackColor]};
    
    if (@available(iOS 13.0, *)) {
        
        UINavigationBarAppearance * appearance = [UINavigationBarAppearance new];
        appearance.backgroundColor = [UIColor whiteColor];
        appearance.titleTextAttributes = attrs;
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    }else{
        [self.navigationController.navigationBar setTitleTextAttributes:attrs];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    }
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 55;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.autoresizingMask = 0xff;
    [self.view addSubview:self.tableView];
    
    
    NSMutableArray * dataList = [NSMutableArray new];
    for(NSInteger i=0; i<NEDConsoleItemType_Count; i++){
        NEDConsoleMenuItem * item = [NEDConsoleMenuItem title:NEDConsoleItemTypeDes(i) type:i className:NEDConsoleItemTypeMapVCClass(i)];
        [dataList addObject:item];
    }
    self.dataSource = dataList;
    [self.tableView reloadData];
    
}

- (void)closeAction{
    [NEDConsole hiddenConsoleMenu];
}


#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = self.dataSource[indexPath.row].title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Class classname = self.dataSource[indexPath.row].className;
    if(classname){
        [self.navigationController pushViewController:[classname new] animated:YES];
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
